﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ross.EntityFramework;
using Ross.EntityFramework.Model;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;


namespace Ross.Service
{
    public class CoursewareService : BaseService, IDisposable
    {
        private TranDbContext dbContext;
        public IRepository<Courseware, int> repository;
        public CoursewareService(string nameOrConnectionString)
        {
            dbContext = new TranDbContext(nameOrConnectionString);
            repository = new Repository<Courseware, int>(dbContext);
        }
        public void Dispose()
        {
            dbContext.Dispose();
        }
        //获取列表
        public ResultDto<List<Dto.Courseware>> GetAllList(long courseId)
        {
            ResultDto<List<Dto.Courseware>> result = new ResultDto<List<Dto.Courseware>>();
            var lists = repository.GetAllList(o => o.CourseID == courseId);
            result.datas = Mapper.Map<List<Dto.Courseware>>(lists);
            return result;
        }
        //删除
        public ResultDto<bool> Delete(int id)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                repository.Delete(id);
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.status = 500;
                result.datas = false;
            }
            return result;
        }
        //添加
        public ResultDto<bool> Create(Dto.Courseware courseware)
        {
            ResultDto<bool> result = new ResultDto<bool>();
            try
            {
                var model = Mapper.Map<Courseware>(courseware);
                var obj = repository.Insert(model);
                result.status = 200;
                result.datas = true;
            }
            catch (Exception ex)
            {
                result.message = ex.Message;
                result.datas = false;
            }
            return result;
        }

        //下载


    }
}
